package com.sansecy.monitor.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.sansecy.monitor.utils.Utils;

/**
 * @Description TODO
 * @Date 2019-08-04 21:09
 * @Created by caoyu
 */
public class ListDialog extends Dialog {

    private ArrayAdapter<String> mAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1);
    private ListView mListView = new ListView(getContext());

    public ListDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mListView);
        mListView.setAdapter(mAdapter);
    }

    public ListView getListView() {
        return mListView;
    }

    public void addHeaderView(View view) {
        mListView.addHeaderView(view);
    }

    public void removeHeaderView(View view) {
        mListView.removeHeaderView(view);
    }


    public void addFooterView(View view) {
        mListView.addFooterView(view);
    }

    public void removeFooterView(View view) {
        mListView.removeFooterView(view);
    }

    public void addData(String s) {
        mAdapter.add(s);
    }

    public void clear() {
        mAdapter.clear();
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        mListView.setOnItemClickListener(listener);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window win = getWindow();
        DisplayMetrics dm = new DisplayMetrics();
        // 一定要设置Background，如果不设置，window属性设置无效
        assert win != null;
//        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        win.getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams params = win.getAttributes();
        //设置dialog高度
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog宽度
        params.width = (int) (Utils.getScreenWidth(getContext()) * 0.85);
        win.setAttributes(params);
    }
}
